//
//  UITapView.m
//  easymarketing
//
//  Created by zhangnorman on 15/2/26.
//  Copyright (c) 2015年 cubead. All rights reserved.
//

#import "UITapView.h"

@interface UITapView() {
    CGPoint beginPoint;
}

@property (nonatomic, copy) void(^tapActions)(id);
@property (nonatomic, strong) UIColor *bgColor;
@property (nonatomic, strong) UIColor *highLightBgColor;

@end

@implementation UITapView

/**
 *  添加
 *
 *  @param action <#action description#>
 */
-(void)addTargetsBlock:(void (^)(id))action{
    self.tapActions = action;

}

/**
 *  设置背景颜色和点击后的高亮颜色
 *
 *  @param backgroundColor          <#backgroundColor description#>
 *  @param highLightBackgourndColor <#highLightBackgourndColor description#>
 */
- (void)setBgColor:(UIColor *)backgroundColor andHighLightBgColor: (UIColor*)highLightBackgourndColor {
    self.bgColor = backgroundColor;
    self.backgroundColor = backgroundColor;
    self.highLightBgColor = highLightBackgourndColor;
}



#pragma 处理点击事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    self.backgroundColor = self.highLightBgColor;
}

/**
 *
 *
 *  @param touches <#touches description#>
 *  @param event   <#event description#>
 */
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint points = [touch locationInView:self];
    // 当用户按住屏幕滑动超过一定距离后，恢复视图的原始颜色
    if (fabs(points.x - beginPoint.x) > 2 || fabs(points.y - beginPoint.y) > 2) {
        self.backgroundColor = self.bgColor;
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    self.backgroundColor = self.bgColor;
    UITouch *touch = [touches anyObject];
    CGPoint points = [touch locationInView:self];
    if (points.x >= 0 && points.y >= 0 && points.x <= self.frame.size.width && points.y <= self.frame.size.height)
    {
        if (self.tapActions) {
            self.tapActions(self);
        }
    }
}



@end
